package com.serena.da.rest.services.plugin;

import org.json.JSONArray;
import org.json.JSONObject;
import com.serena.da.rest.http_helpers.Response;
import com.serena.da.rest.services.SraService;

import java.util.HashMap;

/**
 * Created by Spitkovskyi Igor on 7/30/2014.
 */
public class SraPluginService extends SraService {
    private static SraPluginService pluginService;

    private final String SERVICE_NAME = "plugin/";

    private final String METHOD_STATUS_PLUGIN = "statusPlugin/";
    private final String METHOD_AUTOMATION_PLUGIN = "automationPlugin/";
    private final String METHOD_COMMAND = "command/";
    private final String METHOD_SOURCE_CONFIG_PLUGIN = "sourceConfigPlugin/";


    protected SraPluginService() throws Exception {
        super();
    }

    public static SraPluginService getInstance() throws Exception{
        if(pluginService == null){
            pluginService = new SraPluginService();
        }
        return pluginService;
    }

    public String getRestUrl (String methodName){
        return super.getRestUrl(SERVICE_NAME + methodName, false);
    }

    public String getRestUrl (String methodName, boolean useUrlAuthentication){
        //String SERVICE_TYPE = "plugin/";
        return super.getRestUrl(SERVICE_NAME + methodName, useUrlAuthentication);
    }

    public String getRestUrlAutomationPlugin(){
        return getRestUrl(METHOD_AUTOMATION_PLUGIN);
    }
    public String getRestUrlStatusPlugin(){
        return getRestUrl(METHOD_STATUS_PLUGIN);
    }
    public String getRestUrlCommand(){
        return getRestUrl(METHOD_COMMAND);
    }
    public String getRestUrlSourceConfigPlugin(){
        return getRestUrl(METHOD_SOURCE_CONFIG_PLUGIN);
    }

    private JSONArray getPluginsInfo(String url){
        JSONArray pluginInfo;
        try {
            Response response = executeRequest(url);//sendRequest(getRequest);
            String responseBodyStr = response.getResponseBody();
            pluginInfo = new JSONArray(responseBodyStr);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return pluginInfo;
    }

    //Needed when loading "Create Component" form to understand which source config plugins are available in SRA server.
    public JSONObject getSourceConfigPluginInfoByName (String pluginName){
        String name = "name";

        JSONObject pluginInfo = null;

        String restUrl = getRestUrlSourceConfigPlugin();
        JSONArray allPlugins = getPluginsInfo(restUrl);

        if(allPlugins.length() == 0){
            return null;
        }

        for (int i = 0; i < allPlugins.length(); i++) {
            JSONObject item = allPlugins.getJSONObject(i);
            if (item.getString(name).toLowerCase().contains(pluginName.toLowerCase())) {
                pluginInfo = item;
                break;
            }
        }

        return pluginInfo;
    }

    public HashMap<String, String> getAutomationPluginInfoByName (String pluginName) throws Exception{
        //  http://localhost:8081/serena_ra/rest/plugin/automationPlugin/all
        String id = "id";
        String pluginID = "pluginId";
        String name = "name";
        String version = "version";
        String versionNumber = "versionNumber";
        String ghostedDate = "ghostedDate";

        HashMap<String, String> pluginInfo = new HashMap<String, String>();
        String restUrl = getRestUrlAutomationPlugin() + "all"; //getRestUrl(METHOD_AUTOMATION_PLUGIN + "all");
        JSONArray allPlugins = getPluginsInfo(restUrl);

        if(allPlugins.length() == 0){
            return null;
        }

        for (int i = 0; i < allPlugins.length(); i++) {
            JSONObject item = allPlugins.getJSONObject(i);
            if (item.getString(name).equals(pluginName)) {
                pluginInfo.put(id, item.get(id).toString());
                pluginInfo.put(pluginID, item.get(pluginID).toString());
                pluginInfo.put(name, item.get(name).toString());
                pluginInfo.put(version, item.get(version).toString());
                pluginInfo.put(versionNumber, item.get(versionNumber).toString());
                pluginInfo.put(ghostedDate, item.get(ghostedDate).toString());
                break;
            }
        }
        //HttpGet getRequest = Request.GET(restUrl);
        //GetRequest getRequest = new GetRequest(restUrl);  //HttpGet getRequest = new HttpGet(restUrl);
        /*
        try {
            Response response = executeRequest(restUrl);//sendRequest(getRequest);
            String responseBodyStr = response.getResponseBody();
            JSONArray allPlugins = new JSONArray(responseBodyStr);
            for (int i = 0; i < allPlugins.length(); i++) {
                JSONObject item = allPlugins.getJSONObject(i);
                if (item.getString(name).equals(pluginName)) {
                    pluginInfo.put(id, item.get(id).toString());
                    pluginInfo.put(pluginID, item.get(pluginID).toString());
                    pluginInfo.put(name, item.get(name).toString());
                    pluginInfo.put(version, item.get(version).toString());
                    pluginInfo.put(versionNumber, item.get(versionNumber).toString());
                    pluginInfo.put(ghostedDate, item.get(ghostedDate).toString());
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        */
        return pluginInfo;
    }

    //Gets ID of plugin command, having name of plugin and name of command.
    public String getPluginCommandID (String PluginID, String commandName) throws Exception{
        //  http://localhost:8081/serena_ra/rest/plugin/automationPlugin/b431e3fc-3c82-4ec8-b0a0-1af5e5530968/commands\
        //HashMap<String, String> pluginInfo = getPluginInfoByName(PluginName);
        //String PluginID = pluginInfo.get("id").toString();

        String commandID = "";

        String commandsUrl = getRestUrlAutomationPlugin() + PluginID + "/commands"; //getRestUrl(METHOD_AUTOMATION_PLUGIN + pluginID + "/commands");

        //HttpGet getRequest = Request.GET(commandsUrl);
        //GetRequest getRequest = new GetRequest(commandsUrl);      //HttpGet getRequest = new HttpGet(commandsUrl);
        try {
            Response response = executeRequest(commandsUrl);//sendRequest(getRequest);
            String responseBodyStr = response.getResponseBody();

            JSONArray allCommands = new JSONArray(responseBodyStr);

            for (int i = 0; i < allCommands.length(); i++) {
                JSONObject item = allCommands.getJSONObject(i);
                if (item.getString("name").equalsIgnoreCase(commandName)) {
                    commandID = item.getString("id");
                    break;
                }
            }

            if (commandID.isEmpty()) {
                throw new Exception("Could not find ID of plugin command for specified command name.");
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return commandID;
    }
}
